/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* FlexTextArea.java
*
* Created on November 6, 2007, 6:37 PM
 */
package javautil.swing.components;

import javautil.io.IOUtil;
import javautil.swing.SwingUtil;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;

import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author  stewari1
 */
public class FlexTextArea extends javax.swing.JPanel implements PropertyChangeListener {


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JScrollPane jScrollPane1;

    /** Field description */
    private javax.swing.JTextArea outputJTextArea;

    /** Field description */
    private javax.swing.JButton saveBtn;


    // End of variables declaration//GEN-END:variables

    /** Creates new form FlexTextArea */
    public FlexTextArea() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1    = new javax.swing.JScrollPane();
        outputJTextArea = new javax.swing.JTextArea();
        saveBtn         = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Details"));
        outputJTextArea.setColumns(20);
        outputJTextArea.setEditable(false);
        outputJTextArea.setLineWrap(true);
        outputJTextArea.setRows(5);
        outputJTextArea.setWrapStyleWord(true);
        outputJTextArea.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane1.setViewportView(outputJTextArea);
        saveBtn.setText("Save As");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }

        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap().addComponent(saveBtn)));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addComponent(
                    jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 238,
                    Short.MAX_VALUE).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(saveBtn)));

    }    // </editor-fold>//GEN-END:initComponents

    public void propertyChange(PropertyChangeEvent evt) {

        if (ProgressSupport.PROP_LOG_INFO.equals(evt.getPropertyName())) {
            setLog((String) evt.getNewValue());
        }
    }

    /**
     * Method description
     *
     * @param log description
     */
    private void setLog(final String log) {

        Runnable runnable = new Runnable() {

            public void run() {
                outputJTextArea.setText(log);
            }
        };

        SwingUtilities.invokeLater(runnable);
    }

    /**
     * Method description
     *
     * @param evt description
     */
    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_saveBtnActionPerformed

        if (outputJTextArea.getText().length() > 0) {

            File file = SwingUtil.getFileFromSaveAs(this,
                            new FileNameExtensionFilter("Text files", "txt", "dat"));

            if (file != null) {

                try {
                    IOUtil.writeToFile(file, outputJTextArea.getText());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

        } else {
            SwingUtil.showErrorDialog(this, "Output is empty!");
        }

    }    // GEN-LAST:event_saveBtnActionPerformed

    public void setText(String text) {

        outputJTextArea.setText(text);
        outputJTextArea.setCaretPosition(outputJTextArea.getText().length() - 1);
    }
}
